package rongyao;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class Friend {

    public static void main(String[] args) throws Exception {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        String s = buf.readLine();
        int T = Integer.parseInt(s);
        StringBuilder sb = new StringBuilder();
        for (int x = 0; x < T; x++){
            StringBuilder sb2 = new StringBuilder();
            PriorityQueue<Node> heap = new PriorityQueue<>( (o1, o2) -> {
                if (o2.weight != o1.weight)
                    return o1.weight - o2.weight;
                return o2.code - o1.code;
            });
            s = buf.readLine();
            String[] ss = s.split(" ");
            int m = Integer.parseInt(ss[0]);
            int i = Integer.parseInt(ss[1]);
            int n = Integer.parseInt(ss[2]);
            int[][] graph = new int[m][m];
            s = buf.readLine();
            ss = s.split(" ");
            int k = Integer.parseInt(ss[0]);
            for (int j = 0; j < k; j++){
                int row = Integer.parseInt(ss[j*3 + 1]);
                int col = Integer.parseInt(ss[j*3 + 2]);
                int weight = Integer.parseInt(ss[j*3 + 3]);
                graph[row][col] = weight;
                graph[col][row] = weight;
            }
            boolean[] visited = new boolean[m];
            Queue<Node> queue = new LinkedList<>();
            queue.offer(new Node(i, 0, 0));
            visited[i] = true;
            while (!queue.isEmpty()){
                Node node = queue.poll();
                if (node.level < n){
                    for (int b = 0; b < m; b++){
                        if (graph[node.code][b] != 0 && !visited[b]){
                            visited[b] = true;
                            queue.offer(new Node(b, node.level + 1, node.weight + graph[node.code][b]));
                        }
                    }
                } else if (node.level == n){
                    heap.offer(node);
                }
            }

            while (!heap.isEmpty()){
                Node node = heap.poll();
                sb2.append(node.code).append(" ");
            }
            sb2.deleteCharAt(sb2.length() - 1);
            //sb.reverse();
            sb.append(sb2.reverse().toString()).append("\n");
        }
        System.out.println(sb.toString());
    }
}
class Node{
    int code;
    int level;
    int weight;

    Node(int code, int level, int weight){
        this.code = code;
        this.level = level;
        this.weight = weight;
    }
}
